Day 1: The Tyranny of the Rocket Equation


In [ ]:
with open('input.txt', 'rt') as f:
    nums = [int(l.strip()) for l in f.readlines()]

In [ ]:
def compute_fuel(m):
    return m // 3 - 2

In [ ]:
# test

list(map(compute_fuel, [12, 14, 1969, 100756]))

In [ ]:
# output part 1

sum(map(compute_fuel, nums))

In [ ]:
def iterative_compute_fuel(m):
    res, x = 0, m
    while x > 0:
        x = compute_fuel(x)
        res += max(x, 0)
    return res

In [ ]:
# test

list(map(iterative_compute_fuel, [14, 1969, 100756]))

In [ ]:
# output part 2

sum(map(iterative_compute_fuel, nums))